﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MusicAcademy
{
    public partial class ListOfPersons : Form
    {
        public int SelectedID = -1;

        public ListOfPersons()
        {
            InitializeComponent();
        }

        public byte PersonType { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }

        private void ListOfPersons_Load(object sender, EventArgs e)
        {
            personTableAdapter.Fill(musicAcademyDataSet.Person, PersonType, Program.academyID, "", "", "");
            GrdPerson.ClearSelection();
        }

        private void txtName_TextChanged(object sender, EventArgs e)
        {
            if (chkContainsSearch.Checked)
                personTableAdapter.Fill(musicAcademyDataSet.Person, PersonType, Program.academyID, txtName.Text, txtLastName.Text, "");
            else
                personTableAdapter.FillByStarting(musicAcademyDataSet.Person, PersonType, Program.academyID, txtName.Text, txtLastName.Text, "");
        }

        private void txtLastName_TextChanged(object sender, EventArgs e)
        {
            if (chkContainsSearch.Checked)
                personTableAdapter.Fill(musicAcademyDataSet.Person, PersonType, Program.academyID, txtName.Text, txtLastName.Text, "");
            else
                personTableAdapter.FillByStarting(musicAcademyDataSet.Person, PersonType, Program.academyID, txtName.Text, txtLastName.Text, "");
        }

        private void chkContainsSearch_CheckedChanged(object sender, EventArgs e)
        {
            if (chkContainsSearch.Checked)
                personTableAdapter.Fill(musicAcademyDataSet.Person, PersonType, Program.academyID, txtName.Text, txtLastName.Text, "");
            else
                personTableAdapter.FillByStarting(musicAcademyDataSet.Person, PersonType, Program.academyID, txtName.Text, txtLastName.Text, "");
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (GrdPerson.SelectedRows.Count > 0)
            {
                SelectedID = Convert.ToInt32(GrdPerson.SelectedRows[0].Cells["ID"].Value);
                FirstName = GrdPerson.SelectedRows[0].Cells["firstName"].Value.ToString();
                LastName= GrdPerson.SelectedRows[0].Cells["lastName"].Value.ToString();
                DialogResult = System.Windows.Forms.DialogResult.OK;
                this.Close();
            }
            else
            {
                MessageBox.Show("لطفا یک نفر را انتخاب نمایید.", "خطا", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            SelectedID = -1;
            DialogResult = System.Windows.Forms.DialogResult.Cancel;
        }

        private void GrdPerson_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex == -1)
                return;
            SelectedID = Convert.ToInt32(GrdPerson.Rows[e.RowIndex].Cells["ID"].Value);
            FirstName = GrdPerson.SelectedRows[0].Cells["firstName"].Value.ToString();
            LastName = GrdPerson.SelectedRows[0].Cells["lastName"].Value.ToString();
            DialogResult = System.Windows.Forms.DialogResult.OK;
            this.Close();
        }
    }
}
